﻿using System;
using System.IO;

namespace AppEszköz.Xml
{
    public class XmlFileWriter : IDisposable
    {
        private string xmlFile;
        private int maxFileLength;
        private string rootElementName;
        private TextWriter tw;

        public int MaxFileLength
        {
            get
            {
                return this.maxFileLength;
            }
            set
            {
                this.maxFileLength = value;
            }
        }

        public XmlFileWriter(string xmlFile)
          : this(xmlFile, "XmlFile")
        {
        }

        public XmlFileWriter(string xmlFile, string rootElementName)
        {
            if (string.IsNullOrEmpty(xmlFile))
                throw new ArgumentNullException("xmlFile");
            this.xmlFile = xmlFile;
            this.rootElementName = rootElementName;
            this.maxFileLength = 5000;
            if (this.FileExists())
            {
                TextReader textReader = (TextReader)new StreamReader((Stream)File.OpenRead(xmlFile));
                string str1 = textReader.ReadToEnd();
                textReader.Close();
                string str2 = str1.Replace("</" + rootElementName + ">", "");
                this.tw = (TextWriter)new StreamWriter(xmlFile);
                this.tw.WriteLine(str2);
                this.Flush();
            }
            else
                this.CreateFile();
        }

        public XmlFileWriter(string xmlFile, string rootElementName, int maxFileLength)
          : this(xmlFile, rootElementName)
        {
            this.MaxFileLength = maxFileLength;
        }

        public void Write(string xmlNode)
        {
            if (string.IsNullOrEmpty(this.xmlFile))
                throw new MissingFieldException("XmlFileWriter", "XmlFile");
            if (this.maxFileLength == 0)
                throw new MissingFieldException("XmlFileWriter", "MaxFileLength");
            if (string.IsNullOrEmpty(this.rootElementName))
                throw new MissingFieldException("XmlFileWriter", "RootElementName");
            this.RotateFiles();
            this.tw.WriteLine(xmlNode);
        }

        private bool FileExists()
        {
            return File.Exists(this.xmlFile);
        }

        private void CreateFile()
        {
            string str = "<" + (object)this.rootElementName + " created=\"" + (string)(object)DateTime.Now + "\">";
            this.tw = (TextWriter)new StreamWriter(this.xmlFile);
            this.tw.WriteLine(str);
        }

        private void RotateFiles()
        {
            if (new FileInfo(this.xmlFile).Length <= (long)(this.maxFileLength * 1000))
                return;
            this.Close();
            this.tw = (TextWriter)null;
            string str1 = this.xmlFile.Substring(this.xmlFile.LastIndexOf("."));
            string str2 = this.xmlFile.Substring(0, this.xmlFile.Length - str1.Length);
            if (File.Exists(str2 + "-9" + str1))
                File.Delete(str2 + "-9" + str1);
            for (int index = 8; index > 0; --index)
            {
                if (File.Exists(string.Concat(new object[4]
                {
          (object) str2,
          (object) "-",
          (object) index,
          (object) str1
                })))
                    File.Copy(string.Concat(new object[4]
                    {
            (object) str2,
            (object) "-",
            (object) index,
            (object) str1
                    }), string.Concat(new object[4]
                    {
            (object) str2,
            (object) "-",
            (object) (index + 1),
            (object) str1
                    }), 1 != 0);
            }
            File.Copy(this.xmlFile, str2 + "-1" + str1, true);
            this.CreateFile();
        }

        public void Flush()
        {
            this.tw.Flush();
        }

        public void Close()
        {
            this.tw.WriteLine("</" + this.rootElementName + ">");
            this.tw.Close();
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize((object)this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing || this.tw == null)
                return;
            this.Close();
        }
    }
}
